package com.daisuxia.back.utils;

import java.util.List;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.daisuxia.back.sms.Dest;
import com.daisuxia.back.sms.XmlDto;
import com.daisuxia.chuanglan.sms.request.SmsSendRequest;
import com.daisuxia.chuanglan.sms.response.SmsSendResponse;
import com.daisuxia.chuanglan.sms.util.ChuangLanSmsUtil;
import com.daisuxia.constant.Constant;

/**
 * 发送短信
 * @author gaoyuhai
 * 2016-6-17 下午03:50:44
 */
public class SMS253Util {
	
	private static Logger loger = Logger.getLogger(SMS253Util.class);
/**
 * 组装验证码短信
 * @param telephone
 * @param sms
 * @return
 * @throws  
 */
	/*public static boolean sendSms(String telephone,String sms){
		loger.info("sendSms:"+telephone+"   sms="+sms);
		
		String appsecret = "&app_secret="+SmsConfigConstant.getConstant("appSecret");
		
		XmlDto xmlDto = new XmlDto();
		Head head = new Head();
		head.setApp_key(SmsConfigConstant.getConstant("appkey"));
		head.setNonce_str(SmsConfigConstant.getConstant("nonce_str"));
		head.setSign("");
		head.setTime_stamp(DateUtil.getDateFormat("yyyyMMddHHmmss"));
		
		String keys = DateUtil.getDateFormat("yyyyMMddHHmmss");
		Dest dest = new Dest();
		dest.setDest_id(telephone);
		dest.setMission_num(Constant.VERIFY_CODE+keys);
		List<Dest> list = new ArrayList<Dest>();
		list.add(dest);
		Dests ds = new Dests();
		ds.setDest(list);
		Body body = new Body();
		body.setBatch_num(SmsConfigConstant.getConstant("nonce_str")+keys);
		body.setDests(ds);
		body.setContent(Constant.SMS_SIGN+Constant.SMS_VERIFY_CONTENT.replace("#cont#", sms));
		body.setSms_type(Constant.VERIFY_CODE);
		xmlDto.setHead(head);
		xmlDto.setBody(body);
		
		String smsXml = sortString(xmlDto)+appsecret;
		loger.info("md5-befor:"+smsXml);
		String smsSign = "";
		
		try{
			smsSign = new MD5codingLowCase().MD5(String.valueOf(smsXml));
		}catch(Exception e){
			loger.info("md5-error:"+smsSign);
		}
		loger.info("md5-after:"+smsSign);
		xmlDto.getHead().setSign(smsSign);
		
		String stringXml= XmlUtil.bulidMessage(xmlDto);
		String requestXml = XML_HEAD+stringXml;
		loger.info("sendsms:"+telephone+" content:"+xmlDto);
		try {
			HttpUtil http=new HttpUtil();
			String result = http.doPost(SmsConfigConstant.getConstant("apiurl"), requestXml);
			loger.info("sendSms-result:"+result);
			if(null!=result){
				Result heads = new Result();
				for(String s : heads.getOutString()){
					result = result.replace(s, "");
				}
			}else{
				loger.info("sendsms-result-null");
			}
			Result r = (Result) XmlUtil.readMessage(Result.class, result);
			if(null!=r){
				if(r.getError_code().equals(Constant.SMS_SEND_SUCC)){
					return true;
				}else{
					loger.info("sendsms-result-Error_code:"+r.getError_code());
				}
			}else{
				loger.info("sendsms-result-xml-null");
			}
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
	*/
	/**
	 * 自定义短信内容
	 * @param telephone手机号
	 * @param content内容
	 * @return
	 * @throws  
	 */
		/*public static boolean sendSmsDiy(String telephone,String content){
			loger.info("sendSms:"+telephone+"   sms="+content);
			
			String appsecret = "&app_secret="+SmsConfigConstant.getConstant("appSecret");
			
			XmlDto xmlDto = new XmlDto();
			Head head = new Head();
			head.setApp_key(SmsConfigConstant.getConstant("appkey"));
			head.setNonce_str(SmsConfigConstant.getConstant("nonce_str"));
			head.setSign("");
			head.setTime_stamp(DateUtil.getDateFormat("yyyyMMddHHmmss"));
			
			String keys = DateUtil.getDateFormat("yyyyMMddHHmmss");
			Dest dest = new Dest();
			dest.setDest_id(telephone);
			dest.setMission_num(Constant.VERIFY_CODE+keys);
			List<Dest> list = new ArrayList<Dest>();
			list.add(dest);
			Dests ds = new Dests();
			ds.setDest(list);
			Body body = new Body();
			body.setBatch_num(SmsConfigConstant.getConstant("nonce_str")+keys);
			body.setDests(ds);
			body.setContent(Constant.SMS_SIGN+ content);
			body.setSms_type(Constant.VERIFY_CODE);
			xmlDto.setHead(head);
			xmlDto.setBody(body);
			
			String smsXml = sortString(xmlDto)+appsecret;
			loger.info("md5-befor:"+smsXml);
			String smsSign = "";
			
			try{
				smsSign = new MD5codingLowCase().MD5(String.valueOf(smsXml));
			}catch(Exception e){
				loger.info("md5-error:"+smsSign);
			}
			loger.info("md5-after:"+smsSign);
			xmlDto.getHead().setSign(smsSign);
			
			String stringXml= XmlUtil.bulidMessage(xmlDto);
			String requestXml = XML_HEAD+stringXml;
			loger.info("sendsms:"+telephone+" content:"+xmlDto);
			try {
				HttpUtil http=new HttpUtil();
				String result = http.doPost(SmsConfigConstant.getConstant("apiurl"), requestXml);
				loger.info("sendSms-result:"+result);
				if(null!=result){
					Result heads = new Result();
					for(String s : heads.getOutString()){
						result = result.replace(s, "");
					}
				}else{
					loger.info("sendsms-result-null");
				}
				Result r = (Result) XmlUtil.readMessage(Result.class, result);
				if(null!=r){
					if(r.getError_code().equals(Constant.SMS_SEND_SUCC)){
						return true;
					}else{
						loger.info("sendsms-result-Error_code:"+r.getError_code());
					}
				}else{
					loger.info("sendsms-result-xml-null");
				}
			} catch (ClientProtocolException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return false;
		}*/
	/**
	 * 排序
	 * @param xmlDto
	 * @return
	 */
	@SuppressWarnings("unused")
	private static String sortString(XmlDto xmlDto){
		if(null!=xmlDto){
			StringBuffer sb = new StringBuffer();
			sb.append("app_key="+xmlDto.getHead().getApp_key())
			.append("&batch_num="+xmlDto.getBody().getBatch_num())
			.append("&content="+xmlDto.getBody().getContent());
			List<Dest> list = xmlDto.getBody().getDests().getDest();
			StringBuffer sb0 = new StringBuffer();
			for(Dest d : list){
				sb0.append("&dest_id="+d.getDest_id());
			}
			for(Dest d : list){
				sb0.append("&mission_num="+d.getMission_num());
			}
			sb.append(sb0)
			.append("&nonce_str="+xmlDto.getHead().getNonce_str())
			.append("&sms_type="+xmlDto.getBody().getSms_type())
			.append("&time_stamp="+xmlDto.getHead().getTime_stamp());
			
			return sb.toString();
		}
		return null;
	}
	/**
	 * 创蓝组装验证码短信
	 * @param telephone
	 * @param sms
	 * @return
	 */
	public static boolean sendSmsCL(String telephone,String sms){
		loger.info("sendSms:"+telephone+"   sms="+sms);
		
		// 普通短信地址
		String smsSingleRequestServerUrl = SmsConfigConstant.getConstant("apiurl");
		String account = SmsConfigConstant.getConstant("account");
		String pswd = SmsConfigConstant.getConstant("pswd");
		// 短信内容
	    String msg = Constant.SMS_SIGN+Constant.SMS_VERIFY_CONTENT.replace("#cont#", sms);
		
		SmsSendRequest smsSingleRequest = new SmsSendRequest(account, pswd, msg, telephone );
		String requestJson = JSON.toJSONString(smsSingleRequest);
		loger.info("before request string is: " + requestJson);
		
		String response = ChuangLanSmsUtil.sendSmsByPost(smsSingleRequestServerUrl, requestJson);
		loger.info("response after request result is :" + response);
		if(null!=response){
			SmsSendResponse r = JSON.parseObject(response, SmsSendResponse.class);
			if(r.getCode().equals(Constant.SMS_SEND_SUCC)){
				return true;
			}else{
				loger.info("sendsms-result-Error_code:"+r.getCode());
				loger.info("sendsms-result-Error_errorMsg:"+r.getErrorMsg());
			}
		}else{
			loger.info("sendsms-response-null");
		}
		return false;
	}
	
	
	/**
	 * 创蓝 自定义短信内容
	 * @param telephone手机号
	 * @param content内容
	 * @return
	 * @throws  
	 */
		public static boolean sendSmsDiyCL(String telephone,String content){
			loger.info("sendSms:"+telephone+"   sms="+content);
			
			// 普通短信地址
			String smsSingleRequestServerUrl = SmsConfigConstant.getConstant("apiurl");
			String account = SmsConfigConstant.getConstant("account");
			String pswd = SmsConfigConstant.getConstant("pswd");
			// 短信内容
		    String msg = Constant.SMS_SIGN+ content;
			
			SmsSendRequest smsSingleRequest = new SmsSendRequest(account, pswd, msg, telephone );
			String requestJson = JSON.toJSONString(smsSingleRequest);
			loger.info("before request string is: " + requestJson);
			
			String response = ChuangLanSmsUtil.sendSmsByPost(smsSingleRequestServerUrl, requestJson);
			loger.info("response after request result is :" + response);
			if(null!=response){
				SmsSendResponse r = JSON.parseObject(response, SmsSendResponse.class);
				if(r.getCode().equals(Constant.SMS_SEND_SUCC)){
					return true;
				}else{
					loger.info("sendsms-result-Error_code:"+r.getCode());
					loger.info("sendsms-result-Error_errorMsg:"+r.getErrorMsg());
				}
			}else{
				loger.info("sendsms-response-null");
			}
			return false;
			
		}
}
